//#define __DEBUG__
// by Xeno
#define THIS_FILE "fn_adselchanged.sqf"
#include "x_setup.sqf"

#include "x_uiinc.sqf"

#define __ctrl(vctrl) _ctrl = _display displayCtrl vctrl
#define __ctrlinfo(vctrl) _ctrlinfo = _display displayCtrl vctrl
#define __CTRL2(A) (_display displayCtrl A)
private ["_ctrl", "_display", "_ctrlinfo", "_selection", "_control", "_selectedIndex", "_strp", "_unit", "_posunit", "_sel", "_endtime"];

disableSerialization;

if (!(serverCommandAvailable "#shutdown") && {isMultiplayer}) exitWith {
	[QGVAR(p_f_b_k), [player, GVAR(name_pl), 3]] call FUNC(NetCallEventCTS);
};

PARAMS_1(_selection);

_control = _selection select 0;
_selectedIndex = _selection select 1;

if (_selectedIndex == -1) exitWith {};

_control ctrlEnable false;
_strp = _control lbData _selectedIndex;

_unit = __getMNsVar2(_strp);
GVAR(a_d_cur_uid) = getPlayerUID _unit;
GVAR(a_d_cur_unit_name) = name _unit;
__TRACE_1("adselchanged","_unit");
GVAR(u_r_inf) = nil;
_display = __uiGetVar(GVAR(ADMIN_DLG));
GVAR(a_d_cur_name) = _control lbText _selectedIndex;
__ctrlinfo(1002);
_ctrlinfo ctrlSetText format [(localize "STR_DOM_MISSIONSTRING_689"), GVAR(a_d_cur_name)];
[QGVAR(g_p_inf), [player, GVAR(a_d_cur_uid)]] call FUNC(NetCallEventCTS);

[QGVAR(admin_marker), [0,0,0],"ICON","ColorBlack",[1,1],"",0,"hd_dot"] call FUNC(CreateMarkerLocal);
QGVAR(admin_marker) setMarkerTextLocal GVAR(a_d_cur_name);
_posunit = visiblePositionASL _unit;
QGVAR(admin_marker) setMarkerPosLocal _posunit;

__ctrl(11010);

_ctrl ctrlmapanimadd [0.0, 1.00, getPosASL (vehicle player)];
_ctrl ctrlmapanimadd [1.2, 1.00, _posunit];
_ctrl ctrlmapanimadd [0.5, 0.30, _posunit];
ctrlmapanimcommit _ctrl;

_endtime = time + 30;
waitUntil {!isNil QGVAR(u_r_inf) || {!GVAR(admin_dialog_open)} || {!alive player} || {time > _endtime}};

GVAR(u_r_inf) = GVAR(u_r_inf) select 1;

if (count GVAR(u_r_inf) == 0) exitWith {};

if (!GVAR(admin_dialog_open) || {!alive player} || {time > _endtime}) exitWith {};

_control ctrlEnable true;

if (count GVAR(u_r_inf) == 0) exitWith {_ctrlinfo ctrlSetText format [(localize "STR_DOM_MISSIONSTRING_690"), GVAR(a_d_cur_name)]};

_ctrlinfo ctrlSetText format [(localize "STR_DOM_MISSIONSTRING_691"), GVAR(a_d_cur_name)];

__CTRL2(1003) ctrlSetText GVAR(a_d_cur_name);
__CTRL2(1004) ctrlSetText GVAR(a_d_cur_uid);
__CTRL2(1005) ctrlSetText str(_unit);

_sel = 7;
__CTRL2(1006) ctrlSetText str(GVAR(u_r_inf) select _sel);
__CTRL2(1009) ctrlSetText str(score _unit);
__CTRL2(1007) ctrlEnable ((GVAR(u_r_inf) select _sel) >= 1);
__CTRL2(1008) ctrlEnable (GVAR(a_d_cur_name) != GVAR(name_pl));
__CTRL2(1010) ctrlEnable (GVAR(a_d_cur_name) != GVAR(name_pl));